package org.apache.jmeter.test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class SubSample extends AbstractSampler{
	public static final Logger log=LoggingManager.getLoggerForClass();
	 public static final String text1 = "1"; //$NON-NLS-1$
	 public static final String text2 = "2"; //$NON-NLS-1$
	 private static final Set<String> APPLIABLE_CONFIG_CLASSES = new HashSet<>(
	            Arrays.asList(
	                  
	                    "test.SubSampleDefaultConfigGui"
	            ));
	//private String b="1";
	//private String a="1";
	public String getA() {
		return  getPropertyAsString(text1);
	}


	public void setA(String a) {
		this.setProperty(this.text1, a);
	}


	public String getB() {
		return getPropertyAsString(text2);
	}


	public void setB(String b) {
		this.setProperty(this.text2, b);;;
	}
	@Override
	public SampleResult sample(Entry arg0) {
		//trace("parctiseDemo01Sample");
		SampleResult res=new SampleResult();
		
		boolean isOK=false;//判断sample是否正常运行
		String response;
		res.setSampleLabel("test2");//这里有于设置产生的新的subSample名称
		
		res.sampleStart();
		try {
			
			res.setResultFileName("test1");
			
			int aa=Integer.valueOf(getA());
			int bb=Integer.valueOf(getB());
//			int aa=Integer.valueOf("1");
//			int bb=Integer.valueOf("2");
			int sum=aa+bb;
			response="当前线程是："+Thread.currentThread().getName()+"\r\n"+sum;
			res.setResponseData(response,"utf-8");
			res.setSamplerData("这里是samplerdata");
			res.setDataType(SampleResult.TEXT);
			res.setResponseCodeOK();
			res.setResponseMessage("程序运行正常");
			isOK=true;
			
		} catch (Exception e) {
			res.setResponseCode("500");
			res.setResponseMessage("ab都要输入int型数字啊");
			res.setResponseData("hehe",null);
		}
		//log.info("1111111111111");
		res.sampleEnd();
		res.setSuccessful(isOK);
		return res;
	}
	@Override
    public boolean applies(ConfigTestElement configElement) {
        String guiClass = configElement.getProperty(TestElement.GUI_CLASS).getStringValue();
        return APPLIABLE_CONFIG_CLASSES.contains(guiClass);
    }
}

